const {
	Controller
} = require('uni-cloud-router')
const uniID = require('uni-id')
const dbHelper = require('db-helper')
module.exports = class StoreController extends Controller {
	async getServiceList() {
		let {
			city,
			userLocation,
			token,
			keyword,
			action,
			categoryId
		} = this.ctx.data
		if (userLocation) {
			if (isNaN(parseFloat(userLocation.latitude)) || isNaN(parseFloat(userLocation.longitude))) {
				return {
					code: 500,
					msg: '参数错误'
				}
			}
		}
		return this.service.store.getServiceList({
			userLocation,
			token,
			city,
			keyword,
			action,
			categoryId
		})
	}
	
	async getStoreDetail() {
		let {
			userLocation,
			token,
			storeId
		} = this.ctx.data
		if (!storeId) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		if (userLocation) {
			if (isNaN(parseFloat(userLocation.latitude)) || isNaN(parseFloat(userLocation.longitude))) {
				return {
					code: 500,
					msg: '参数错误'
				}
			}
		}
		return this.service.store.getStoreDetail({
			userLocation,
			token,
			storeId
		})
	}
  
  
  async screenplaytStore() {
    let {data, location} = this.ctx.data
    
    return await this.service.store.screenplaytStore({data, location})
  }
  
  // 获取门禁列表
  async getDoorList() {
    let { userId, storeId, pageNum, pageSize } = this.ctx.data
    
    return this.service.store.getDoorList(userId, storeId, pageNum, pageSize)
  }
  
  // 保存门禁
  async saveDoor() {
    let { storeId, storeName, doorName, sn, _id } = this.ctx.data
    
    if (!storeId && !storeName && !doorName) return { code: 500, msg: '参数错误' }
    
    return this.service.store.saveDoor(storeId, storeName, doorName, sn, _id)
  }
  
  // 删除门禁
  async delDoor() {
    let { doorId, sn } = this.ctx.data
    
    if (!doorId) return { code: 500, msg: '参数错误' }

    return this.service.store.delDoor(doorId, sn)
  }
  
  // 解绑门禁设备
  async unbindSN() {
    let { sn, id } = this.ctx.data
    
    if (!id) return { code: 500, msg: '参数错误' }
    
    return this.service.store.unbindSN(sn, id)
  }
}
